package com.ipt.frm.pw.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ipt.frm.bean.Alumno;
import com.ipt.frm.pw.listener.PropertiesListener;

/**
 * Servlet implementation class ListaAlumnos
 */
public class ListaAlumnos extends HttpServlet {
	private static final long serialVersionUID = 3789740005433276970L;

	/*
	 * private static DAOFactory fMySQL; private static InterfaceAlumnoDAO aDao;
	 */

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListaAlumnos() {
		super();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {

		super.init(config);

		/*
		 * fMySQL = DAOFactory.getDAOFactory(DAOFactory.MYSQL); aDao =
		 * fMySQL.getInterfaceAlumnoDAO();
		 */
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ArrayList<Alumno> alumnos = null;

		response.setContentType("application/json");
		response.setCharacterEncoding(StandardCharsets.UTF_8.toString());

		PrintWriter out = response.getWriter();
		try {
			alumnos = PropertiesListener.aDao.buscarAlumno(
					"%" + request.getParameter("nombre") + "%", "alumno");
			/*
			 * Iterator<Alumno> it = alumnos.iterator(); String nombreAlumno =
			 * "";
			 * 
			 * String nombre; String apellido1; String apellido2; Alumno alumno
			 * = null;
			 * 
			 * while (it.hasNext()) { alumno = it.next();
			 * 
			 * nombre = alumno.getNombre(); apellido1 = alumno.getApellido1();
			 * apellido2 = alumno.getApellido2();
			 * 
			 * //nombreAlumno += "<option class=\"opciones\" value=\"" + nombre
			 * // + " " + apellido1 + " " + apellido2 + "\">";
			 * 
			 * }
			 */
			Gson gson = new Gson();
			String json = gson.toJson(alumnos);
			// System.out.println(json);
			out.print(json);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
